package com.ddstation.app.holiday.service;

import com.ddstation.crud.entity.DtInvsInstitutionHolidayEntity;
import com.ddstation.crud.entity.DtInvsInstitutionHolidayWeeklyEntity;
import com.ddstation.crud.mapper.DtInvsInstitutionHolidayMapper;
import com.ddstation.crud.mapper.DtInvsInstitutionHolidayWeeklyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.spring.annotation.MapperScan;

import javax.inject.Inject;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("com.ddstation.crud.mapper")
public class HolidayServiceImpl implements HolidayService {
    @Override
    public List<DtInvsInstitutionHolidayEntity> getHolidaysByDuration(String startDate, String endDate, String institutionId) {
        Example holiExample = new Example(DtInvsInstitutionHolidayEntity.class);
        holiExample.createCriteria().andBetween("date", startDate, endDate).andEqualTo("institutionId", institutionId);
        return dtInvsInstitutionHolidayMapper.selectByExample(holiExample);
    }

    @Override
    public DtInvsInstitutionHolidayWeeklyEntity getInstitutionWeeklyRes(String institutionId) {
        Example weeklyExample = new Example(DtInvsInstitutionHolidayWeeklyEntity.class);
        weeklyExample.createCriteria().andEqualTo("institutionId", institutionId);
        return dtInvsInstitutionHolidayWeeklyMapper.selectOneByExample(weeklyExample);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHoliday(DtInvsInstitutionHolidayEntity oneDay) {

        Example dayExample = new Example(DtInvsInstitutionHolidayEntity.class);
        dayExample.createCriteria().andEqualTo("date", oneDay.getDate());
        DtInvsInstitutionHolidayEntity oldDay = dtInvsInstitutionHolidayMapper.selectOneByExample(dayExample);
        if (null == oldDay) {
            dtInvsInstitutionHolidayMapper.insertSelective(oneDay);
        } else {
            oneDay.setId(null);
            dtInvsInstitutionHolidayMapper.updateByExampleSelective(oneDay, dayExample);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWeeklyRes(DtInvsInstitutionHolidayWeeklyEntity weeklyRes) {
        if (null == weeklyRes.getId()) {
            dtInvsInstitutionHolidayWeeklyMapper.insertSelective(weeklyRes);
        } else {
            dtInvsInstitutionHolidayWeeklyMapper.updateByPrimaryKeySelective(weeklyRes);
        }
        return true;
    }

    @Override
    public boolean checkHoliday(String date, String institutionId) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(date, dtf);
        Example holiExample = new Example(DtInvsInstitutionHolidayEntity.class);
        holiExample.createCriteria().andEqualTo("date", date);
        DtInvsInstitutionHolidayEntity entity = dtInvsInstitutionHolidayMapper.selectOneByExample(holiExample);
        Example weeklyExample = new Example(DtInvsInstitutionHolidayWeeklyEntity.class);
        weeklyExample.createCriteria().andEqualTo("institutionId", institutionId);
        DtInvsInstitutionHolidayWeeklyEntity weeklyEntity = dtInvsInstitutionHolidayWeeklyMapper.selectOneByExample(weeklyExample);
        if (null != entity && entity.getIsHoliday() == 1) {
            return true;
        }
        if (null != weeklyEntity) {
            switch (localDate.getDayOfWeek()) {
                case MONDAY:
                    return weeklyEntity.getMonday() == 1;
                case TUESDAY:
                    return weeklyEntity.getTuesday() == 1;
                case WEDNESDAY:
                    return weeklyEntity.getWednesday() == 1;
                case THURSDAY:
                    return weeklyEntity.getThursday() == 1;
                case FRIDAY:
                    return weeklyEntity.getFriday() == 1;
                case SATURDAY:
                    return weeklyEntity.getSaturday() == 1;
                case SUNDAY:
                    return weeklyEntity.getSunday() == 1;
                default:
                    return false;
            }
        } else {
            return localDate.getDayOfWeek().equals(DayOfWeek.SATURDAY) || localDate.getDayOfWeek().equals(DayOfWeek.SUNDAY);
        }
    }

    @Override
    public List<DayOfWeek> getResWeekdays(String institutionId) {
        DtInvsInstitutionHolidayWeeklyEntity weeklyModel = getInstitutionWeeklyRes(institutionId);
        List<DayOfWeek> resWeekdays = new ArrayList<>();
        if (null != weeklyModel) {
            if (weeklyModel.getMonday() > 0) {
                resWeekdays.add(DayOfWeek.MONDAY);
            }
            if (weeklyModel.getTuesday() > 0) {
                resWeekdays.add(DayOfWeek.TUESDAY);
            }
            if (weeklyModel.getWednesday() > 0) {
                resWeekdays.add(DayOfWeek.WEDNESDAY);
            }
            if (weeklyModel.getThursday() > 0) {
                resWeekdays.add(DayOfWeek.THURSDAY);
            }
            if (weeklyModel.getFriday() > 0) {
                resWeekdays.add(DayOfWeek.FRIDAY);
            }
            if (weeklyModel.getSaturday() > 0) {
                resWeekdays.add(DayOfWeek.SATURDAY);
            }
            if (weeklyModel.getSunday() > 0) {
                resWeekdays.add(DayOfWeek.SUNDAY);
            }
        } else {
            resWeekdays.add(DayOfWeek.SUNDAY);
            resWeekdays.add(DayOfWeek.SATURDAY);
        }
        return resWeekdays;
    }

    @Inject
    DtInvsInstitutionHolidayMapper dtInvsInstitutionHolidayMapper;

    @Inject
    DtInvsInstitutionHolidayWeeklyMapper dtInvsInstitutionHolidayWeeklyMapper;
}
